package com.jinhua.admin.dao.impl;
import com.jinhua.mybatis.util.MybatisPlus;
import com.jinhua.mybatis.util.MybatisPlusBuild;
import com.jinhua.config.IdUtil;
import com.jinhua.admin.dao.OrderDao;
import com.jinhua.admin.entity.OrderPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
* 订单表的dao层
*
*/

@Repository
public class OrderDaoImpl implements OrderDao {
        private final int PAGE_SIZE = 10;
    @Override
    public int saveOrderIfNotExist(OrderPo orderPo) {
        return orderPo.save();
    }
    @Override
    public int modOrder(OrderPo orderPo) {
        return orderPo.update(orderPo.build().eq(OrderPo::getId, orderPo.getId()));
    }
    @Override
    public int delOrderByIds(Long... ids) {
        OrderPo orderPo = new OrderPo();
        orderPo.setDeletedFlag(Boolean.TRUE);
        return orderPo.update(orderPo.build().in(OrderPo::getId, ids));
    }
    @Override
    public OrderPo findOrderById(Long id) {
        MybatisPlusBuild build = new MybatisPlusBuild(OrderPo.class);
        build.eq(OrderPo::getDeletedFlag, Boolean.FALSE);
        build.eq(OrderPo::getId, id);
        return MybatisPlus.getInstance().findOne( new OrderPo(), build);
    }
    @Override
    public List<OrderPo> findOrderByAll(OrderPo orderPo, Integer page, Integer size) {
        MybatisPlusBuild build = new MybatisPlusBuild(OrderPo.class);
        build.eq(OrderPo::getDeletedFlag, Boolean.FALSE);
        if (orderPo.getId() == null) {
            if (StringUtils.isNotEmpty(orderPo.getUserName())) {
                build.like(OrderPo::getUserName,orderPo.getUserName());
            }
            if (StringUtils.isNotEmpty(orderPo.getGoodsName())) {
                build.like(OrderPo::getGoodsName,orderPo.getGoodsName());
            }
            if (page == null) {
                page = 1;
            }
            IdUtil.setTotal(build);
            build.page(page, size==null?PAGE_SIZE:size);
        }
        else {
            build.eq(OrderPo::getId, orderPo.getId());
        }
        return MybatisPlus.getInstance().findAll( new OrderPo() ,build);
    }
}
